#include "TYSprite.h"

TYSprite::TYSprite(void)
{
	m_bTYIsTouchEnable = false;
	m_nTYPriority = LAYER_HIGHEST_PRIORITY;
	m_bTYSwallowsTouches = true;
}

TYSprite::~TYSprite(void)
{
}



void TYSprite::setPriority (int nPriority)
{
	m_nTYPriority = nPriority;
}


void TYSprite::setSwallowsTouches (bool var)
{
	m_bTYSwallowsTouches = var;
}

void TYSprite::setTYTouchEnable (bool var)
{
	m_bTYIsTouchEnable = var;
}

int TYSprite::getPriority ()
{
	return m_nTYPriority;
}

bool TYSprite::getSwallowsTouches ()
{
	return m_bTYSwallowsTouches;
}

bool TYSprite::getTYTouchEnable ()
{
	return m_bTYIsTouchEnable;
}


CCRect TYSprite::rect ()
{
	CCSize s = this->getContentSize ();
	return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}

CCRect TYSprite::GetWindowRect ()
{
	return CCRectMake(
		m_tPosition.x - (m_tContentSize.width/2),
		m_tPosition.y - (m_tContentSize.height/2),
		m_tContentSize.width,
		m_tContentSize.height);
}

void TYSprite::onEnter ()
{
	if (m_bTYIsTouchEnable)
	{
		CCTouchDispatcher::sharedDispatcher ()->addTargetedDelegate(this, m_nTYPriority, m_bTYSwallowsTouches);
	}
	CCSprite::onEnter ();
}

void TYSprite::onExit ()
{
	if (m_bTYIsTouchEnable)
	{
		CCTouchDispatcher::sharedDispatcher ()->removeDelegate(this);
	}
	CCSprite::onExit ();
}

bool TYSprite::containsTouchLocation (CCSprite* pSpirte, CCTouch* touch)
{
	if (!pSpirte)
	{
		return false;
	}
	CCSize s = pSpirte->getContentSize ();

	return CCRect::CCRectContainsPoint(CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height), pSpirte->convertTouchToNodeSpace (touch));

}

bool TYSprite::containsTouchLocation (CCTouch* touch)
{
	return containsTouchLocation (this,touch);
}

bool TYSprite::ccTouchBegan (CCTouch* touch, cocos2d::CCEvent* event)
{
	if (! containsTouchLocation (touch) )
	{
		return false;
	}

	return true;
}


TYSprite* TYSprite::spriteWithFile (const char *pszFileName)
{
	TYSprite* sprite = new TYSprite();
	if (sprite && sprite->initWithFile(pszFileName) )
	{
		sprite->autorelease();
		return sprite;
	}
	else
	{
		delete sprite;
		sprite = NULL;
		return NULL;
	}
}